<template>
  <a-modal
    :title="`客服产品${model && model.id > 0 ? '编辑' : '新增'}`"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" :labelCol="{ sm: { span: 4 } }" :wrapperCol="{ sm: { span: 20 } }" :selfUpdate="true">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <!-- <a-form-item label="账号">
          <a-input
            v-decorator="['account', { rules: [{ required: true, message: '账号不能为空' }] }]"
            placeholder="请输入账号"
          />
        </a-form-item> -->
        <a-form-item label="所属语言">
          <a-select v-decorator="['lang', { rules: [{ required: true, message: '所属语言不能为空' }] }]" placeholder="请选择所属语言">
            <a-select-option v-for="it in lang" :key="it.value" :value="it.value">{{it.name}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="产品名称">
          <a-input
            v-decorator="['name', { rules: [{ required: true, message: '产品名称不能为空' }] }]"
            placeholder="请输入产品名称"
          />
        </a-form-item>
        <a-form-item label="跳转链接">
          <a-input
            v-decorator="['url', { rules: [{ required: true, message: '跳转链接不能为空' }] }]"
            placeholder="请输入跳转链接"
          />
        </a-form-item>
        <a-form-item label="说明">
          <a-input v-decorator="['remark']" placeholder="请输入说明" />
        </a-form-item>
        <a-form-item label="排序">
          <a-input v-decorator="['sort']" placeholder="请输入排序(数值越小越靠前)" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { lang } from '@/locales'
// 表单字段
const fields = ['name', 'url', 'remark', 'sort', 'id', 'lang']

let _lang = []
for(let i in lang){
  _lang.push({
    value: i,
    name: lang[i],
  })
}

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      lang: _lang
    }
  },
  components: {
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', (n) => {
      if (this.model) {
        this.form.setFieldsValue(pick(this.model, fields))
      }
    })
  },
  methods: {
    
  },
  computed: {
  },
  watch: {},
}
</script>
